package com.android.multiplesmart.activity;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.multiplesmart.R;
import com.android.multiplesmart.adapter.CouresAdapter;
import com.android.multiplesmart.adapter.Tar_CouresAdapter;

import com.android.multiplesmart.bean.CodeBean;
import com.android.multiplesmart.bean.LanguageTypeBean;
import com.android.multiplesmart.bean.Servicess;
import com.android.multiplesmart.databinding.ActivitysMainBinding;
import com.android.multiplesmart.utils.QrCodeUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.schillerchina.baselibrary.constans.AppConstants;
import com.schillerchina.baselibrary.utils.GsonUtil;
import com.schillerchina.baselibrary.utils.Pref;
import com.schillerchina.baselibrary.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;

public class CodeActivity extends BaseActivity {
    private CouresAdapter couresAdapter;
    private Tar_CouresAdapter tar_couresAdapter;
    private List<String> coures_List=new LinkedList<>();
    private List<String> tar_Coures_List=new LinkedList<>();
    private LanguageTypeBean languageTypeBean;
    private RecyclerView course,translate_course;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.code);
        //activitysMainBinding = DataBindingUtil.setContentView(this, R.layout.code);
        initView();

    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.closess:
              finish();
              break;
      }
    }

    @Override
    public void initListener() {

    }

    @Override
    protected void onPause() {
        super.onPause();

    }
    private void initWebSocket() {
//        //启动服务
//        startJWebSClientService();
//        //绑定服务
//        bindService();
          System.out.println("启动了服务");
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                moni();
//            }
//        },5000);
    }

    String message,code,te,yuan;
    TextView original,german,Language,name;
    @Override
    public void initView() {
        message=  getIntent().getStringExtra("Url");
        code=getIntent().getStringExtra("code");
        te=getIntent().getStringExtra("te");
        yuan=getIntent().getStringExtra("yuan");
        original=findViewById(R.id.original);
        german=findViewById(R.id.german);
        Language=findViewById(R.id.Language);
        name=findViewById(R.id.name);
        course=findViewById(R.id.course);
        translate_course=findViewById(R.id.translate_course);
        TextView closess=findViewById(R.id.closess);
        closess.setOnClickListener(this);
        view();
        IntentFilter discoveryFilter = new IntentFilter();
        discoveryFilter.addAction(AppConstants.WEBSOCKET_COLSE_TEACHING_ACTION);
        discoveryFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        registerReceiver(mBluetoothReceiver, discoveryFilter);


    }
    private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, final Intent intent) {
           String  action = intent.getAction();
            if (action.equals(AppConstants.WEBSOCKET_COLSE_TEACHING_ACTION)){//PC主动推送结束授课信号
                    message = intent.getStringExtra("message");
                    view();
            }
        }
    };



    private  void  view(){
        languageTypeBean= GsonUtil.GsonToBean(te, LanguageTypeBean.class);
        if(languageTypeBean!=null){
             if (languageTypeBean.getData()!=null){
                 Language.setText(languageTypeBean.getData().getCourse());
                 name.setText(languageTypeBean.getData().getTeacher());
             }
        }

        System.out.println(message+"------"+code);
         CodeBean codeBean= GsonUtil.GsonToBean(message, CodeBean.class);
              if (codeBean.getData()!=null){
                if (codeBean.getData().getCourseFileUrl()!=null){
                    //ImageView course= findViewById(R.id.img);
                    ///课件

                   if (codeBean.getData().getCourseFileUrl().size()>0){
                    GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);
                    course.setLayoutManager(gridLayoutManager);
                    for(int i=0;i<codeBean.getData().getCourseFileUrl().size();i++){
                        if (codeBean.getData().getCourseFileUrl().get(i).getLanguageCode().equals(code)){
                            coures_List.add(codeBean.getData().getCourseFileUrl().get(i).getUrl());
                        }

                    }
                    couresAdapter=new CouresAdapter(this,coures_List);
                    course.setAdapter(couresAdapter);
//                  original.setText("Download the original courseware");
                    //course.setImageBitmap(QrCodeUtil.createQRCode(coures_List.get(0),200,200));
                   }else {
                       original.setText("");
                   }
//
                }else{
                    original.setText("");
                }

                if (codeBean.getData().getSpeechFileUrl()!=null){
                    //ImageView translate_course= findViewById(R.id.imgone);
                    ///翻译课件
                    if (codeBean.getData().getSpeechFileUrl().size()>0){
                        String languageEnName="";
                        GridLayoutManager gridLayoutManagers=new GridLayoutManager(this,3);
                        translate_course.setLayoutManager(gridLayoutManagers);
                        for(int i=0;i<codeBean.getData().getSpeechFileUrl().size();i++){
                            if (codeBean.getData().getSpeechFileUrl().get(i).getLanguageCode().equals(code)){
                                tar_Coures_List.add(codeBean.getData().getSpeechFileUrl().get(i).getUrl());
                                languageEnName= codeBean.getData().getSpeechFileUrl().get(i).getLanguageEnName();
                            }
                        }
                        //translate_course.setImageBitmap(QrCodeUtil.createQRCode(tar_Coures_List.get(0),200,200));
                        tar_couresAdapter=new Tar_CouresAdapter(this,tar_Coures_List);
                        translate_course.setAdapter(tar_couresAdapter);
                        german.setText("Download "+languageEnName+" courseware");
                    }else{
                        german.setText("");
                    }
//
                }else{
                    german.setText("");
                }
            }else {
                  original.setText("");
                  german.setText("");
            }



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothReceiver);
    }
}
